var rootPath = getRootPath();

$(function() {
	$.ajax({
		url: rootPath + '/resource/selectPageReses',
        type: 'POST',
        contentType : 'application/json',
        data: JSON.stringify({
        	"dr" : 1
        }),
        success: function (data) {
        	fillResource(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	//绑定enter事件
	$("#resName").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchResource").click();
		}
	});
	
	$("#searchResource").click(function() {
		var resName = $("#resName").val();
		$.ajax({
			url: rootPath + '/resource/selectPageReses',
			type: 'POST',
			contentType : 'application/json',
			data: JSON.stringify({
				"dr" : 1,
				"resName" : resName
			}),
			success: function (data) {
				$("#resources").empty();
				fillResource(data);
			},
			error: function (jqXHR, textStatus, errorThrown) {
				layer.alert("error:(");
			}
		});
	});
});

/*资源-添加-编辑*/
function resource_add_edit(title,url,w,h) {
	layer_show(title,url,w,h);
}

function fillResource(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr = "";
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' name='resTr'>";
			tr += "<td>" + data.returnObject.list[i].resName + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].resUrl) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].resDesc) + "</td></tr>";
		}
		$("#resources").empty();
		$("#resources").append(tr);
		
		//填充分页信息
		var page = "";
		page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage(" + 1 + ")'>首页</span>";
		if(data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage(" + data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if(data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>" + data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage(" 
					+ data.returnObject.navigatepageNums[i] + ")'>" 
					+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if(data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage(" 
				+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage(" + data.returnObject.pages + ")'>末页</span>";
		
		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").mouseover(function() {
			$(this).css("cursor", "pointer");
		});
	} else {
		$("#dataNum").html("0");
		layer.alert(data.returnMessage);
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	$.ajax({
		url: rootPath + '/resource/selectPageReses',
		type : 'POST',
		contentType : 'application/json',
		data : JSON.stringify({
			"dr" : 1,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillResource(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("跳转页面失败:(");
		}
	});
}